﻿using System;
using System.Drawing;
using System.IO;

namespace MyLibrary.UI
{
    public abstract class BaseItem
    {
        private string _path = "";
        public string ItemPath
        {
            get { return _path; }
            set { _path = value; }
        }

        public Image Icon { get; set; }

        public long Size { get; set; }

        public DateTime Date { get; set; }

        public abstract string Name
        {
            get;
            set;
        }

        public BaseItem Parent { get; set; }

        private bool _isChecked;
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                if (Owner != null)
                    Owner.OnNodesChanged(this);
            }
        }

        public BaseItem()
        {
            Size = 0;
        }

        public FolderBrowserModel Owner { get; set; }

        public override string ToString()
        {
            return _path;
        }
    }

    public class RootItem : BaseItem
    {
        public RootItem(string name, FolderBrowserModel owner)
        {
            ItemPath = name;
            Owner = owner;
        }

        public override string Name
        {
            get
            {
                return ItemPath;
            }
            set
            {
            }
        }
    }

    public class FolderItem : BaseItem
    {
        public override string Name
        {
            get
            {
                return Path.GetFileName(ItemPath);
            }
            set
            {
                string dir = Path.GetDirectoryName(ItemPath);
                string destination = Path.Combine(dir, value);
                Directory.Move(ItemPath, destination);
                ItemPath = destination;
            }
        }

        public FolderItem(string name, BaseItem parent, FolderBrowserModel owner)
        {
            ItemPath = name;
            Parent = parent;
            Owner = owner;
        }
    }

    public class FileItem : BaseItem
    {
        public override string Name
        {
            get
            {
                return Path.GetFileName(ItemPath);
            }

            set
            {
                var dir = Path.GetDirectoryName(ItemPath);
                var destination = Path.Combine(dir, value);
                File.Move(ItemPath, destination);
                ItemPath = destination;
            }
        }

        public FileItem(string name, BaseItem parent, FolderBrowserModel owner)
        {
            ItemPath = name;
            Parent = parent;
            Owner = owner;
        }
    }
}
